#include "mainwidget.h"
#include "./ui_mainwidget.h"
#include <QIcon>
#include <QHBoxLayout>

MainWidget* MainWidget::instance = nullptr; //记得将静态指针初始化
MainWidget *MainWidget::getInstance()
{
    if (instance == nullptr) {
        // 此处不传入参数, 以桌面为父窗口.
        // 由于此处的窗口是整个程序的主窗口, 父窗口就设定为桌面, 本身就是常规设定.
        instance = new MainWidget();
    }
    return instance;
}

MainWidget::~MainWidget()
{
    delete ui;
}



MainWidget::MainWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("微Q");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));

    //主界面窗口初始化
    InitMainWindow();
    //左侧窗口初始化
    InitLeftWindow();
    //中间窗口初始化
    InitMidWindow();
    //信号槽初始化
    InitSignalSlots();

}
//主界面
void MainWidget::InitMainWindow()
{
    //三个页面都是在同一水平上的所以 采用水平布局
    QHBoxLayout* layout = new QHBoxLayout();

    this->setLayout(layout);
    windowLeft = new QWidget();
    windowMid = new QWidget();
    windowRight = new QWidget();

    windowLeft->setFixedWidth(60);
    windowMid->setFixedWidth(280);
    windowRight->setMinimumHeight(600);
    windowRight->setMinimumWidth(600);

    //设置3个界面的背景色
    windowLeft->setStyleSheet("QWidget { background-color: rgb(46, 46, 46); }");
    windowMid->setStyleSheet("QWidget { background-color: rgb(247, 247, 247); }");
    // windowRight->setStyleSheet("QWidget { background-color: rgb(245, 245, 245); }");

    windowRight->setStyleSheet("QWidget { background-color: rgb(100, 100, 100); }");
    layout->addWidget(windowLeft);
    layout->addWidget(windowMid);
    layout->addWidget(windowRight);

    //细节调整
    layout->setSpacing(0);  // 设置控件之间的间隔为 0
    layout->setContentsMargins(0, 0, 0, 0);  // 设置外边距为 0
}

//左侧窗口
void MainWidget::InitLeftWindow()
{
    QVBoxLayout *layout = new QVBoxLayout();
    layout->setContentsMargins(0,20,0,0); // 左 上 右 下，为距离界面的间隔
    windowLeft->setLayout(layout);

    //设置头像
    headsBtn = new QPushButton();
    headsBtn->setFixedSize(40,40);
    headsBtn->setIconSize(QSize(40,40));
    headsBtn->setIcon(QIcon(":/resource/image/defaultAvatar.png"));
    headsBtn->setFlat(true); //去掉按钮的边框，只是显示图标
    // headsBtn->setMinimumHeight(40); //固定大小

    //设置会话按钮
    sessionBtn = new QPushButton();
    sessionBtn->setFixedSize(40,40);
    sessionBtn->setIconSize(QSize(25,25));
    sessionBtn->setIcon(QIcon(":/resource/image/session_active.png"));
    sessionBtn->setFlat(true);

    //设置好友按钮
    friendBtn = new QPushButton();
    friendBtn->setFixedSize(40,40);
    friendBtn->setIconSize(QSize(25,25));
    friendBtn->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    friendBtn->setFlat(true);

    //设置好友申请按钮
    applyBtn = new QPushButton();
    applyBtn->setFixedSize(40,40);
    applyBtn->setIconSize(QSize(25,25));
    applyBtn->setIcon(QIcon(":/resource/image/apply_inactive.png"));
    applyBtn->setFlat(true);

    //细节调整
    layout->addWidget(headsBtn, 0, Qt::AlignTop | Qt::AlignHCenter); //设置为0，在根据addStretch（）函数，可以当窗口放大控件的大小间隔不发生变化
    layout->addWidget(sessionBtn, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(friendBtn, 0, Qt::AlignTop | Qt::AlignHCenter);
    layout->addWidget(applyBtn, 0, Qt::AlignTop | Qt::AlignHCenter);

    //调整的目标是符合一定美观  不管通过什么方式调整都行只要符合。
    // layout->addWidget(headsBtn,1,Qt::AlignTop | Qt::AlignCenter);
    // layout->addWidget(sessionBtn);
    // layout->addWidget(friendBtn);
    // layout->addWidget(applyBtn);

    layout->setSpacing(5);  // 设置控件之间的间隔为 0
    // layout->setContentsMargins(0, 0, 0, 0);  // 设置外边距为 0
    // layout->setContentsMargins(0, 50, 0, 0);
    layout->addStretch(5); // 添加一个可伸展的空白区域


}

void MainWidget::InitMidWindow()
{
    QGridLayout *layout = new QGridLayout();
    windowMid->setLayout(layout);

    searchEdit = new QLineEdit();
    addBtn = new QPushButton();

    searchEdit->setFixedHeight(25);
    searchEdit->setPlaceholderText("搜索");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 5px; background-color: rgb(226, 226, 226); padding-left: 5px;}");

    addBtn->setFixedSize(25, 25);
    addBtn->setIcon(QIcon(":/resource/image/cross.png"));
    QString style = "QPushButton { border-radius: 5px; background-color: rgb(226, 226, 226); }";
    style += " QPushButton:pressed { background-color: rgb(240, 240, 240); }";
    addBtn->setStyleSheet(style);

    sessionfriendarea = new SessionFriendArea();
    sessionfriendarea->setStyleSheet("QWidget { background-color: rgb(231, 231, 231); }");

    //调整符合页面要求的布局
    //方法1
    layout->addWidget(searchEdit,0,1,Qt::AlignTop);
    layout->addWidget(addBtn,0,3,Qt::AlignTop);
    layout->addWidget(sessionfriendarea,1,0,1,5);
    layout->setContentsMargins(0,10,0,0);


    //方法2
    // QWidget* spacer1 = new QWidget();
    // spacer1->setFixedWidth(10);
    // QWidget* spacer2 = new QWidget();
    // spacer2->setFixedWidth(10);
    // QWidget* spacer3 = new QWidget();
    // spacer3->setFixedWidth(10);

    // layout->addWidget(spacer1, 0, 0);
    // layout->addWidget(searchEdit, 0, 1);
    // layout->addWidget(spacer2, 0, 2);
    // layout->addWidget(addBtn, 0, 3);
    // layout->addWidget(spacer3, 0, 4);
    // layout->addWidget(sessionfriendarea, 1, 0, 1, 5);

}

void MainWidget::InitRightWindow()
{

}

void MainWidget::InitSignalSlots()
{

    connect(sessionBtn,&QPushButton::clicked,this,&MainWidget::OnSessionBtnSlots);
    connect(friendBtn,&QPushButton::clicked,this,&MainWidget::OnFriendBtnSlots);
    connect(applyBtn,&QPushButton::clicked,this,&MainWidget::OnApplyBtnSlots);
}

void MainWidget::OnSessionBtnSlots()
{
    sessionBtn->setIcon(QIcon(":/resource/image/session_active.png"));
    friendBtn->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    applyBtn->setIcon(QIcon(":/resource/image/apply_inactive.png"));
}

void MainWidget::OnFriendBtnSlots()
{
    sessionBtn->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friendBtn->setIcon(QIcon(":/resource/image/friend_active.png"));
    applyBtn->setIcon(QIcon(":/resource/image/apply_inactive.png"));
}

void MainWidget::OnApplyBtnSlots()
{
    sessionBtn->setIcon(QIcon(":/resource/image/session_inactive.png"));
    friendBtn->setIcon(QIcon(":/resource/image/friend_inactive.png"));
    applyBtn->setIcon(QIcon(":/resource/image/apply_active.png"));
}
